package com.leyou.gateway.properties;

import com.leyou.common.auth.entity.UserInfo;
import com.leyou.common.auth.utils.JwtUtils;
import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.security.PublicKey;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/5/15 0015
 * Time: 20:42
 */
@Data
@Slf4j
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {

    /**
     * 公钥地址
     */
    private String publicKeyPath;
    /**
     * 公钥
     */
    private PublicKey publicKey;
    private UserTokenProperties user = new UserTokenProperties();

    @Data
    public class UserTokenProperties {
        /**
         * 存放token的cookie name
         */
        private String cookieName;
        /**
         * 存放用户信息的header名称
         */
        private String headerName;
    }

    /**
     * 微服务认证token
     */
    private PrivilegeTokenProperties privilege = new PrivilegeTokenProperties();

    @Data
    public class PrivilegeTokenProperties {
        /**
         * 微服务认证token的id
         */
        private Long id;
        /**
         * 服务秘钥
         */
        private String secret;

        /**
         * 存放微服务认证token的请求头
         */
        private String headerName;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            //获取公钥
            this.publicKey = RsaUtils.getPublicKey(publicKeyPath);
        } catch (Exception e) {
            log.error("初始化公钥失败", e);
            throw new RuntimeException(e);
        }

    }
}
